package com.first.member.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.TbMember;
import com.first.common.util.OssUtil;
import com.first.common.vo.Result;
import com.first.member.service.MemberService;
import com.first.member.vo.MemberVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

/**
 * @program: first-project
 * @description:
 * @author: 马英贤
 * @create: 2023-05-30 14:55
 **/
@RestController
@Api(tags = "会员信息")
@RequestMapping("/student/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("uploadMsg")
    @ApiOperation("视频上传")
    public String uploadMsg(@PathParam("file") MultipartFile file) {
        return OssUtil.uploadFile(file);
    }

    @GetMapping("tuichuLogin")
    @ApiOperation("前端web界面退出")
    public Result tuichuLogin(HttpServletRequest request) {
        String homeToken = request.getHeader("homeToken");
        redisTemplate.delete("home::" + homeToken);
        if (Boolean.TRUE.equals(redisTemplate.hasKey("home::" + homeToken))) {
            return Result.fail("退出失败");
        }
        return Result.success();
    }

    @PostMapping("getStudentAll/{current}/{pageSize}")
    @ApiOperation(value = "查询数据")
    public Result getStudent(@PathVariable Integer current, @PathVariable Integer pageSize, @RequestBody MemberVo memberVo) {
        //System.out.println(memberVo+"~~~~~~~~~~~~~~~~");
        Page<TbMember> page = memberService.findByPage(current, pageSize, memberVo);
        //System.out.println("=========="+page);
        return Result.success(page);
    }

    @ApiOperation("修改学生状态")
    @PostMapping("stateChange")
    public Result stateChange(@RequestParam String id, @RequestParam Integer isDisabled) {
        Integer integer = memberService.updateIsDisabled(id, isDisabled);
        return Result.success();
    }

    @ApiOperation("修改会员信息")
    @PostMapping("/updateStudent")
    public Result update(@RequestBody TbMember tbMember) {
        TbMember member = new TbMember();
        member.setId(tbMember.getId());
        member.setMobile(tbMember.getMobile());
        member.setSex(tbMember.getSex());
        member.setNickname(tbMember.getNickname());
        member.setRemark(tbMember.getRemark());
        member.setGmtModified(tbMember.getGmtModified());
        memberService.update(member);
        return Result.success();
    }

    @ApiOperation("个人信息")
    @GetMapping("/findStudentBymobile/{mobile}")
    public Result findStudentBymobile(@PathVariable String mobile) {
        TbMember tbMember = memberService.selectOne(mobile);
        return Result.success(tbMember);
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/modifyStudentInformation")
    public Result modifyStudentInformation(@RequestBody TbMember tbMember) {
        Integer integer = memberService.updateUser(tbMember);
        return Result.success(integer);
    }

    @GetMapping("/getYanZhengMa/{mobile}")
    @ApiOperation("验证码信息校验")
    public Result getYanZhengMa(@PathVariable String mobile) {
        return memberService.getYanZhengMa(mobile);
    }


    @GetMapping("/getYanZhengMa1/{mobile}")
    @ApiOperation("个人信息修改验证码获取")
    public Result getYanZhengMa1(@PathVariable String mobile) {
        return memberService.getYanZhengMa1(mobile);
    }


    @PostMapping("/installMember/{mobile}/{qianPassword}")
    @ApiOperation("注册提交")
    public Result installMember(@PathVariable String mobile, @PathVariable String qianPassword) {
        return memberService.installMember(mobile, qianPassword);
    }


    @ApiOperation("修改密码")
    @PostMapping("/updateMemberPassword/{mobile}/{newPassword}")
    public Result updateMemberPassword(@PathVariable String mobile, @PathVariable String newPassword) {
        Integer integer = memberService.updateMemberPassword(mobile, newPassword);
        return Result.success(integer);
    }
}
